<template>
<div>
  <el-form :model="saveshop" label-width="100px" :rules="saveRule" ref="del">
    <el-form-item label="商品名称">
      <el-select v-model="saveshop.sid" clearable placeholder="请选择采购的商品" @change="buyshop">
        <el-option
          v-for="item in shopList"
          :label="item.sname"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="采购数量" prop="storage">
      <el-input v-model="saveshop.num" style="width: 250px" @change="getprice"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="storage">
      <el-input v-model="price" style="width: 250px" disabled ></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="desc">
      <el-input type="textarea" v-model="bz"></el-input>
    </el-form-item>
  </el-form>
    <el-button type="button" @click="reset">取 消</el-button>
    <el-button type="primary" @click="saveThisShop">确 定</el-button>
</div>
</template>

<script>
    import axios from "axios";

    export default {
      name: "AddShop",
      data() {
        return {
          saveshop: {
            bh:null,
            gysid:null,
            sid:null,
            num:null,
            peo:"",
            buytime:new Date(),
            zt:0
          },
          jg:null,
          price:null,
          sum:null,
          bz:"",
          shopList:[],
          uname:"",
          saveRule:{

          }
        }
      },
      methods:{
        reset: function () {
          this.$refs['del'].resetFields();
          this.dialogFormVisible = false;
        },
        buyshop:function () {
          let id=this.saveshop.sid;
          axios.get("http://localhost:8080/shop/find/"+id).then((msg)=>{
            this.saveshop.gysid=msg.data.id;
            this.jg=msg.data.price;
          })
        },
        getprice:function () {
          this.price=this.saveshop.num*this.jg;

        },
        getshop:function () {

          axios.get("http://localhost:8080/shop/slist").then((msg)=>{
            this.shopList=msg.data;
          })
        },
        saveThisShop:function () {
          if (this.saveshop.num>=0){
            axios.post("http://localhost:8080/list/add",this.saveshop).then((msg)=>{
              alert(msg.data.length)
              if (msg.data.length<10){
                alert("成功")
                this.$router.push('UserList');
              }else {
                alert(msg.data)
              }
            })
          }else {
            alert("数量不能为空")
          }

        },
        getUser:function(){
          this.uname=JSON.parse(sessionStorage.getItem("uname"));
          this.saveshop.peo=this.uname;
        }
      },
      mounted() {
        this.getshop();
        this.getUser();
      }
    }
</script>

<style scoped>

</style>
